package mattkrutsick.android.fitnesstracker;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;


public class AddExActivity extends Activity implements OnClickListener{
    /** Called when the activity is first created. */
	String table_name;
	EditText et;
    Button back, add, home;
    AlertDialog alert;
    SQLiteDatabase db;
    int sets=0;
    Spinner spin;
    ArrayAdapter<Integer> adapter;
    
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.addexercise);
        
        //CreateWorkoutActivity x=new CreateWorkoutActivity();
        
        
        et=(EditText)findViewById(R.id.txtexercise);
       //Make references to gui objects
        back=(Button)findViewById(R.id.btngoback);
        add=(Button)findViewById(R.id.btnadd);
        home=(Button)findViewById(R.id.btnhome);
        spin=(Spinner)findViewById(R.id.spinner1);
        back.setOnClickListener(this);
        add.setOnClickListener(this);
        home.setOnClickListener(this);
        
        //table_name=x.getIntent().getStringExtra("table_name");
       //table_name=getIntent().getStringExtra("table_name");
        Bundle bundle=getIntent().getExtras();
        table_name=bundle.getString("table_name");
        
        
        fillSpinner(spin);
        
        //Log.v(table_name,"<-------should say arms-------<<<<");
        
    }

	public void onClick(View v) {
		// TODO Auto-generated method stub
		if(back.isPressed()){
			startActivity(new Intent(AddExActivity.this, HomeScreenActivity.class));
			AddExActivity.this.finish();
		}
		if(home.isPressed()){
			startActivity(new Intent(AddExActivity.this, HomeScreenActivity.class));
			AddExActivity.this.finish();
		}
		
     /*	
		if(add.isPressed()){
			try{
				Log.v("opening","opeing");
				db=this.openOrCreateDatabase("FITNESS", MODE_WORLD_WRITEABLE, null);
				Log.v("done opening","inserting rows");
				db.execSQL("INSERT INTO '"+table_name+"' VALUES('"+et.getText().toString()+"', "+spin.getItemIdAtPosition(spin.getSelectedItemPosition())+", 0, 0);");
				Log.v("done creating table","...");
			}catch(SQLiteException se){
					makeAlert("Could not add Exercise!", "ERROR!", "OK");
			}finally{
				db.close();
				Log.v("db closed","...");
				makeAlert(et.getText().toString()+" is add to your "+table_name+" workout successfully!","Success","OK");
			}		
		}*/	
	}
	


    /*
     * shows dialog boxes when exceptions occur
     */
    public void makeAlert(String message, String title, String button){
		//alert message
	    alert=new AlertDialog.Builder(this).create();
		alert.setTitle(title);
		alert.setMessage(message);
		alert.setButton( button, new DialogInterface.OnClickListener() {
			
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				return;
			}
		});
		alert.show();
	}
	
    
    public void fillSpinner(Spinner s){
    	adapter=new ArrayAdapter<Integer>(this,android.R.layout.simple_spinner_dropdown_item);
    	s.setAdapter(adapter);
    	for(int i=1; i<11; i++){
    		adapter.add(i);
    	}
    }}
	
		    
	        	
